<template>
    <div class="userinformation">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="注册邮箱" prop="username">
                <el-input v-model="ruleForm.username" disabled></el-input>
            </el-form-item>
             <el-form-item label="输入密码" prop="password">
                <el-input v-model="ruleForm.password"></el-input>
            </el-form-item>
             <el-form-item label="笔名" prop="author">
                <el-input v-model="ruleForm.author"></el-input>
            </el-form-item>
            <el-form-item label="头像" prop="headerimg">
                <el-input v-model="ruleForm.headerimg"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交修改</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
  export default {
    data() {
      return {
        ruleForm: {
          password: '',
          username:'',
          name:'',
          headerimg:'',
        },
        rules: {
          username: [
            { required: true, message: '请输入注册邮箱', trigger: 'blur' },
            { min: 5, max: 20, message: '长度在 5到 20 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 5, max: 20, message: '长度在 5到 20 个字符', trigger: 'blur' }
          ],
          author: [
            { message: '请输入呢称,作家必填这项', trigger: 'blur' },
            { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
          ],
           headerimg: [
            { message: '请输入头像地址', trigger: 'blur' },
            { min: 2, max: 255, message: '长度在 2 到 255 个字符', trigger: 'blur' }
          ],
        }
      };
    },
    created(){
      let user=JSON.parse(sessionStorage.getItem("user"))
      this.ruleForm.username=user.username
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {     
            // alert('submit!');
            let data=await this.axios.get("user/userinformation",{params:this.ruleForm})
            if(data.data.status==200){
              this.resetForm('ruleForm')
              return  this.$message.success("修改成功")
              
            }
              return  this.$message.error("修改失败")
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style scoped lang="less">
.userinformation{
    width:100%;
    height: calc(100vh - 100px);
    position: relative;
    .el-form{
        width:400px;
        text-align: center;
        padding-top: 20px;
        background-image: linear-gradient(to top, #cfd9df 0%, #e2ebf0 100%);
        border:2px solid #ccc;
        position: absolute;
        top:50%;
        left:50%;
        transform: translate(-80%,-80%);
        border-radius: 10px;
        .el-form-item{
            height:50px;
            .el-input{
                width:250px;
                height: 30px;
                border-radius: 5px;
            } 
        }
    
    }
}
</style>